package com.cat.binaryTree;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/count-good-nodes-in-binary-tree/https://leetcode.cn/problems/count-good-nodes-in-binary-tree/
 * @create 2025/9/10 10:22
 * @since JDK17
 */

public class Solution09 {
    int dfs(TreeNode root, int max) {
        if (root == null) {
            return 0;
        }
        int ans = max <= root.val ? 1 : 0;
        max = Math.max(max, root.val);
        ans += dfs(root.left, max);
        ans += dfs(root.right, max);
        return ans;
    }

    public int goodNodes(TreeNode root) {
        return dfs(root, Integer.MIN_VALUE);
    }
}
